package com.chixing.ih.module.programmer.controller;

import com.chixing.ih.json.ResponseData;
import com.chixing.ih.module.programmer.pojo.PersonalSkill;
import com.chixing.ih.module.programmer.pojo.dto.PersonalSkillDTO;
import com.chixing.ih.module.programmer.service.IPersonalSkillService;
import com.chixing.ih.module.programmer.service.IProgrammerService;
import com.chixing.ih.util.RequestIdUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @version 1.0
 * @Author MasterYan
 * @Date 2020/10/3
 * Time: 11:21
 */
@RestController
@RequestMapping("/personalSkill")
@Api(tags = "程序员模块个人技能相关Api")
public class PersonalSkillController {


    @Autowired
    IPersonalSkillService personalSkillService;

    @Autowired
    IProgrammerService programmerService;

    /**
     * desc:批量保存技能接口
     *
     * @param personalSkills
     * @return
     */
    @ApiOperation(value = "批量保存技能接口", notes = "批量保存技能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "技能id", required = true, dataType = "long"),
            @ApiImplicitParam(name = "skillName", value = "技能名称", dataType = "string"),
            @ApiImplicitParam(name = "skillScore", value = "技能分数", dataType = "int")

    })
    @ApiResponses({
            @ApiResponse(code = 201, message = "保存成功"),
            @ApiResponse(code = 400, message = "保存失败"),
    })

    @PutMapping("/personalSkill")
    public Map<String, Object> updateListPerSkills(@RequestBody List<PersonalSkill> personalSkills, HttpServletRequest request) {
        Long myId = RequestIdUtil.getMyId(request);
        boolean flag = personalSkillService.updateList(personalSkills);
        if (flag) {
            programmerService.updateProgrammerTime(myId);
            return ResponseData.REP_CREATED_SUCCESS();
        } else
            return ResponseData.REP_INVALID_ERROR();


    }

    /**
     * desc:查询技能接口
     *
     * @return
     */
    @ApiOperation(value = "查询技能接口", notes = "查询用户所有技能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "程序员id", required = true, dataType = "long"),

    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "查询失败"),
    })
    @GetMapping("/personalSkill/{userId}")
    public Map<String, Object> listPerSkills(@PathVariable("userId") long userId, HttpServletRequest request) {
        List<PersonalSkill> personalSkills = personalSkillService.listAll(userId);
        return ResponseData.REP_GET_SUCCESS(personalSkills);

    }

    /**
     * desc:获取新增个人技能经历id接口
     *
     * @return
     */
    @ApiOperation(value = "获取新增个人技能经历id接口", notes = "获取新增个人技能经历id")
    @ApiResponses({
            @ApiResponse(code = 200, message = "新增成功"),
            @ApiResponse(code = 400, message = "新增失败"),
    })


    @PostMapping("/personalSkill")
    public Map<String, Object> savePerSkill(HttpServletRequest request) {
        Long myId = RequestIdUtil.getMyId(request);
        PersonalSkillDTO personalSkillDTO = new PersonalSkillDTO();
        personalSkillDTO.setUserId(myId);
        PersonalSkill personalSkill = personalSkillService.savePersonalSkill(personalSkillDTO);
        if (personalSkill != null) {
            programmerService.updateProgrammerTime(myId);
            return ResponseData.REP_GET_SUCCESS(personalSkill);
        } else
            return ResponseData.REP_INVALID_ERROR();

    }

    /**
     * desc:删除一条技能接口
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一条技能接口", notes = "删除一条技能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "技能id", required = true, dataType = "long"),

    })
    @ApiResponses({
            @ApiResponse(code = 201, message = "删除成功"),
            @ApiResponse(code = 400, message = "删除失败"),
    })


    @DeleteMapping("/personalSkill/{id}")
    public Map<String, Object> deletePerSkill(@PathVariable("id") long id, HttpServletRequest request) {
        Long myId = RequestIdUtil.getMyId(request);
        boolean flag = personalSkillService.deletePersonalSkill(id);
        if (flag) {
            programmerService.updateProgrammerTime(myId);
            return ResponseData.REP_CREATED_SUCCESS();
        } else
            return ResponseData.REP_INVALID_ERROR();

    }


}
